var options,mapPanel,vector,filtroVulnerabilita,filtroComune,filtroFiume,filtroCombinato,filterStrategy,highlightControl;
var extent = new OpenLayers.Bounds(12.35,43.12,13.35,44.12);
var map = new OpenLayers.Map(options);


//Definizione layer
var vmap = new OpenLayers.Layer.WMS("Vmap0",
			             "http://vmap0.tiles.osgeo.org/wms/vmap0",
     				     {layers:'basic'}
);
var comuni = new OpenLayers.Layer.WMS("Confini comunali",
                                        "http://hydrogate.unipg.it/cgi-bin/mapserverwms",
                                        {layers:'comuni',transparent:true},
                                        {isBaseLayer:false,visibility:false}
);
var orto2006 = new OpenLayers.Layer.WMS("Ortofoto 2006",
                                        "http://geo.umbriaterritorio.it/arcgis/services/public/ORTOFOTO_2005_WGS84/MapServer/WMSServer",
                                        {layers:'0'},
                                        {isBaseLayer:true,visibility:false}
);
//Filtri ponti

filtroVulnerabilita = new OpenLayers.Filter.Comparison({
    type: OpenLayers.Filter.Comparison.EQUAL_TO,
    property: "vulnerabilita",
    value: ''
});

filtroComune = new OpenLayers.Filter.Comparison({
    type: OpenLayers.Filter.Comparison.EQUAL_TO,
    property: "comune",
    value: '' 
});

filtroFiume = new OpenLayers.Filter.Comparison({
    type: OpenLayers.Filter.Comparison.EQUAL_TO,
    property: "fiume",
    value: '' 
});

filtroCombinato = new OpenLayers.Filter.Logical({
    type: OpenLayers.Filter.Logical.AND,
    filters: [] //Parte vuoto così mostra tutte le features    
});

filterStrategy = new OpenLayers.Strategy.Filter({filter:filtroCombinato});

vector = new OpenLayers.Layer.Vector("Ponti",{strategies:[filterStrategy],displayInLayerSwitcher:false});
OpenLayers.loadURL('pontiCompleto.geojson','', this, function(response){
                                                                var format = new OpenLayers.Format.GeoJSON();
                                                                var features = format.read(response.responseText);
                                                                vector.addFeatures(features);
});

var report = function(e) {
                    OpenLayers.Console.log(e.type, e.feature.id);
};

var showPopup = function(e) {
                    popup = new OpenLayers.Popup.Anchored("chicken", 
                    e.feature.geometry.getBounds().getCenterLonLat(),
                    null,
                    "<div style='font-size:.6em'>Fiume: " + e.feature.attributes.fiume +"<br>Catastale: " + e.feature.attributes.catastale +"<br>Comune:" +e.feature.attributes.comune + "</div>",
                    null, true);
                    popup.setSize(new OpenLayers.Size(300,80));
                    e.feature.popup = popup;
                    map.addPopup(popup);
                    //Evidenzia le righe
                    var index = grid.store.find('catastale',e.feature.attributes.catastale);
                    grid.getSelectionModel().selectRow(index);
}

var hidePopup = function(e) {
                    map.removePopup(e.feature.popup);
                    e.feature.popup.destroy();
                    e.feature.popup = null;
                    grid.selModel.clearSelections();
}

highlightCtrl = new OpenLayers.Control.SelectFeature(vector, {
                    hover: false,
                    highlightOnly: true,
                    renderIntent: "temporary",
                    eventListeners: {
                                        featurehighlighted: showPopup,
                                        featureunhighlighted: hidePopup,
                    }
});

var selectCtrl = new OpenLayers.Control.SelectFeature(vector,
                     {clickout: true}
);

map.addControl(highlightCtrl);
map.addControl(new OpenLayers.Control.LayerSwitcher());
highlightCtrl.activate();


//Mostra la pagina finito di caricare il layer
vmap.events.register("loadend",vmap , function() {
       if (Ext.get('loading')) {
       Ext.get('loading').remove();
       Ext.get('loading-mask').fadeOut({remove:true}); 
       }
});

mapPanel = new GeoExt.MapPanel({
        title: "Cartografia",
        map: map,
        center: new OpenLayers.LonLat(12.45, 43.09),
        zoom: 9,
	    layers: [vmap,vector,comuni,orto2006],
        extent: extent
});
